﻿using CmccTool.KPI.DbTables;
using CmccTool.KPI.Services;
using SQLite;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static CmccTool.KPI.DbTables.KpiTables;


namespace CmccTool.KPI.View
{
    public partial class FrmErrNode : Form
    {

        private readonly SQLiteConnection? _db = KpiImpHelper.KpiDbChh();
        private KpiTables.ErrCell _errCell = null!;
        private KpiTables.ErrNode _errNode = null!;
        private string _sta = string.Empty;

        private ErrCellAnalysis _errCellAnalysis = null!;

        #region 窗体初始化
        public FrmErrNode()
        {
            InitializeComponent();
        }

        public FrmErrNode(string states)
        {
            _sta = states;
            InitializeComponent();
        }


        private void FrmErrNode_Load(object sender, EventArgs e)
        {
            cmbProblemLevel1.Items.AddRange(["故障", "纠纷", "用户", "规划建设", "优化原因","其他"]);//问题一级
            cmbProblemLevel2.Items.Clear();//问题二级
            cmbMeasureType.Items.AddRange("故障处理", "设备拆除", "沟通协调", "观察", "网调整改", "后台修复", "后台排查", "前台扫频","现场测试", "维护排查", "小区合并","修改资管", "其他");//措施类型

            cmbResponsibleDept.Items.AddRange(["优化室", "维护室", "工程室"]);//责任室
            cmbResponsibleSpecialty.Items.Clear();//责任专业
            cmbResponsiblePerson.Items.AddRange(["胡赟", "彭家华", "刘浩", "殷玉林", "粱其君", "丁显强", "涂清豪"]);//责任人

            cmbStatus.Items.AddRange(["分发处理", "已处理", "已确认", "重新处理"]);//状态
            cmbIsSchool.Items.AddRange(["是", "否"]);//是否学校


            //
            cmbProblemLevel1.MouseDown += ComboBox_MouseDown;
            cmbProblemLevel2.MouseDown += ComboBox_MouseDown;
            cmbMeasureType.MouseDown += ComboBox_MouseDown;
            cmbResponsibleSpecialty.MouseDown += ComboBox_MouseDown;
            cmbResponsibleDept.MouseDown += ComboBox_MouseDown;
            cmbResponsiblePerson.MouseDown += ComboBox_MouseDown;
            cmbStatus.MouseDown += ComboBox_MouseDown;
            cmbIsSchool.MouseDown += ComboBox_MouseDown;

        }
        #endregion

        #region 数据映射
        // 根据一级级原因 获取对应的二级子项
        private string[] getProblemLevel2(string ProblemLevel1)
        {
            return ProblemLevel1 switch
            {
                "故障" => ["设备", "链路", "传输", "动力", "无维护条件", "闭锁"],
                "纠纷" => ["辐射", "物业", "停业", "放假"],
                "用户" => ["放假", "潮汐", "新兴区域","用户少"],
                "规划建设" => ["规划","设计", "施工"],
                "优化原因" => ["统计问题", "干扰闭锁", "质差闭锁", "参数原因"],
                "其他" => ["资管问题"],
                _ => Array.Empty<string>() // 默认返回空数组
            };
        }

        // 根据二级原因 获取对应的措施类型
        private string GetMeasureType(string ProblemLevel1, string ProblemLevel2)
        {

            switch (ProblemLevel1)
            {
                case "故障":
                    if (ProblemLevel2 == "无维护条件") { return "沟通协调"; } else { return "故障处理"; }
                case "纠纷":
                    if (ProblemLevel2 == "停业") { return "设备拆除"; } else { return "沟通协调"; }
                case "用户":
                    return "观察";
                case "规划建设":
                    return "网调整改";
                case "优化原因":
                    return ProblemLevel2 switch
                    {
                        "统计问题" => "后台修复",
                        "干扰闭锁" => "后台排查",
                        "质差闭锁" => "后台排查",
                        "参数原因" => "调整参数",
                        _ => "其他"
                    };
            }

            return "其他"; // 默认返回其他
        }

        private string[] GetResponsibleSpecialty(string ResponsibleDept)
        {
            return ResponsibleDept switch
            {
                "优化室" => ["优化"],
                "维护室" => ["无线", "传输", "外市电", "网调(室分)", "网调(宏站)", "铁塔","资管"],
                "工程室" => ["工程(室分)", "工程(宏站)", "传输", "外市电", "铁塔"],
                _ => Array.Empty<string>() // 默认返回空数组
            };
        }

        private string getResponsiblePerson(string ResponsibleDept, string ResponsibleSpecialty, string ProblemLevel2)
        {
            // 根据责任室、责任专业和二级原因获取责任人
            switch (ResponsibleDept)
            {
                case ("优化室"):
                    return ProblemLevel2 switch
                    {
                        "统计问题" => "丁显强",
                        "干扰闭锁" => "涂清豪",
                        "质差闭锁" => "涂清豪",
                        _ => "丁显强" // 默认返回王小明
                    };

                case "维护室":
                    return ResponsibleSpecialty switch
                    {
                        "无线" => "胡赟",
                        "传输" => "彭家华",
                        "外市电" => "刘浩",
                        "网调(室分)" => "胡赟",
                        "网调(宏站)" => "胡赟",
                        "铁塔" => "胡赟",
                        _ => "胡赟"// 默认返回空字符串
                    };
                case "工程室":
                    return ResponsibleSpecialty switch
                    {
                        "工程(室分)" => "殷玉林",
                        "工程(宏站)" => "殷玉林",
                        "传输" => "殷玉林",
                        "外市电" => "殷玉林",
                        "铁塔" => "殷玉林",
                        _ => "殷玉林" // 默认返回空字符串
                    };
            }



            return string.Empty; // 默认返回空字符串
        }


        #endregion

        #region cmb 关联逻辑/cmb SelectedIndexChanged / dtp ValueChanged


        private void cmbProblemLevel1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // 根据一级原因更新二级原因
            cmbProblemLevel2.Items.Clear();
            cmbProblemLevel2.SelectedIndex = -1; // 清除选中状态
            cmbProblemLevel2.Items.AddRange(getProblemLevel2(cmbProblemLevel1.Text));
        }

        private void cmbResponsibleDept_SelectedIndexChanged(object sender, EventArgs e)
        {
            // 根据责任室更新责任专业
            cmbResponsibleSpecialty.Items.Clear();
            cmbResponsibleSpecialty.SelectedIndex = -1; // 清除选中状态
            cmbResponsibleSpecialty.Items.AddRange(GetResponsibleSpecialty(cmbResponsibleDept.Text));
        }

        private void cmbProblemLevel2_SelectedIndexChanged(object sender, EventArgs e)
        {
            // 根据一级和二级原因更新措施类型
            cmbMeasureType.SelectedIndex = -1; // 清除选中状态
            cmbMeasureType.Text = GetMeasureType(cmbProblemLevel1.Text, cmbProblemLevel2.Text);
        }

        private void cmbResponsibleSpecialty_SelectedIndexChanged(object sender, EventArgs e)
        {

            cmbResponsiblePerson.Text = getResponsiblePerson(cmbResponsibleDept.Text, cmbResponsibleSpecialty.Text, cmbProblemLevel2.Text);
        }


        private void cmbStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbStatus.Text == "重新处理")
            {
                dtpConfirmTime.Value = dtpConfirmTime.MinDate;
            }

        }

        private void dtpProcessTime_ValueChanged(object sender, EventArgs e)
        {
            if (dtpProcessTime.Value.Year > 2000)
            {
                if (dtpConfirmTime.Value >= dtpProcessTime.Value)
                {
                    cmbStatus.Text = "已确认";
                }
                else
                {
                    cmbStatus.Text = "已处理";
                }
            }
            else
            {
                cmbStatus.Text = "分发处理";

            }
        }

        #endregion

        // 更新按钮点击事件
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (_db == null)
            {
                MessageBox.Show("数据库连接失败！");
                return;
            }

            if (IsValidInput())
            {
                _errNode = GetErrNode();

                switch (_sta)
                {
                    case "new":
                        _db.RunInTransaction(() => { _db.InsertOrReplace(_errNode); });

                        // 新增时才会有关联ErrCell的关系
                        if (_errCell != null)
                        {
                            KpiTables.ErrCellNodeRelation rel = new()
                            {
                                ErrCellKeyId = _errCell.KeyId,
                                ErrNodeKeyId = _errNode.KeyId,
                                KeyId = $"{_errNode.KeyId}_{_errCell.KeyId}"
                            };
                            _db.RunInTransaction(() => { _db.InsertOrReplace(rel); });
                        }
                        break;
                    case "update":
                        _db.RunInTransaction(() => { _db.Update(_errNode); });
                        break;
                }

                // 更新关联 ErrCell 的状态
                _errCellAnalysis.updateErrCellByNodeKey(_errNode.KeyId);


                this.Close();

                //MessageBox.Show("完成");

                _errCellAnalysis.updateLeftUp();
            }
        }

        // 检查所有必填项是否已填写
        private bool IsValidInput()
        {



            foreach (Control control in this.tableLayoutPanel1.Controls)
            {

                if (control is TextBox textBox && string.IsNullOrWhiteSpace(textBox.Text))
                {
                    switch (control.Name)
                    {
                        case "txbCGI" or "txbConfirmPerson":
                            continue;
                        default:
                            MessageBox.Show($"请填写 {textBox.Name} 字段");
                            return false;
                    }
                }
                else if (control is ComboBox comboBox && string.IsNullOrWhiteSpace(comboBox.Text))
                {
                    MessageBox.Show($"请填写 {comboBox.Name} 字段");
                    return false;
                }

            }

            return true;
        }

        // 从表单控件获取 ErrNode 对象
        private KpiTables.ErrNode GetErrNode()
        {


            DateTime ProcessTime = dtpProcessTime.Value;
            string strProcessTime = ProcessTime.Year > 2000 ? ProcessTime.ToString("yyyy-MM-dd") : string.Empty;

            DateTime ConfirmTime = dtpConfirmTime.Value;
            string strConfirmTime = ConfirmTime.Year > 2000 ? ConfirmTime.ToString("yyyy-MM-dd") : string.Empty;

            KpiTables.ErrNode errNode = new KpiTables.ErrNode
            {
                KeyId = txbKeyId.Text.Trim(),
                ENI = txbENI.Text.Trim(),
                GcNodeName = txbGcNodeName.Text.Trim(),
                CGI = txbCGI.Text.Trim(),
                StartDate = dtpStartDate.Value.ToString("yyyy-MM-dd"),
                Problem = txbProblem.Text.Trim(),

                ProblemLevel1 = cmbProblemLevel1.Text,
                ProblemLevel2 = cmbProblemLevel2.Text,
                MeasureType = cmbMeasureType.Text,
                Measure = txbMeasure.Text.Trim(),
                ResponsibleDept = cmbResponsibleDept.Text,
                ResponsibleSpecialty = cmbResponsibleSpecialty.Text,
                ResponsiblePerson = cmbResponsiblePerson.Text,
                ProcessTime = strProcessTime,
                Status = cmbStatus.Text,
                ConfirmTime = strConfirmTime,
                ConfirmPerson = txbConfirmPerson.Text.Trim(),
                IsSchool = cmbIsSchool.Text == "是" ? 1 : 0,

                ErrCellCount = 1


            };
            return errNode;
        }

        // 通过 ErrCell 设置表单控件值
        internal void SetErrCell(KpiTables.ErrCell selectedErrCell, ErrCellAnalysis errCellAnalysis = null!)
        {
            if (_db == null)
            {
                MessageBox.Show("数据库连接失败！");
                return;
            }

            if (errCellAnalysis == null)
            {
                _errCellAnalysis = new ErrCellAnalysis();
            }
            else
            {
                _errCellAnalysis = errCellAnalysis;
            }

            _errCell = selectedErrCell;
            // 设置表单控件的值
            txbKeyId.Text = $@"{DateTime.Now.ToString("yyyy-MM-dd")}_{_errCell.ENI}"; // 键值ID
            txbENI.Text = _errCell.ENI;
            txbGcNodeName.Text = _db.ExecuteScalar<string>(@$"SELECT 工参基站 FROM GcCgiInfo WHERE ENI = '{selectedErrCell.ENI}'") ?? string.Empty;
            //dtpStartDate.Value = DateTime.TryParse(_errCell.StartDate, out DateTime startDate) ? startDate : DateTime.Now;
            dtpStartDate.Value = DateTime.Now;
            if (dtpProcessTime.Value.Year < 2000) { dtpProcessTime.Value = DateTime.Parse("1990-01-01"); }

            txbKeyId.Enabled = false; // 键值ID不可编辑
            txbENI.Enabled = false; // ENI不可编辑
            dtpStartDate.Enabled = false; // 开始日期不可编辑

            cmbIsSchool.Text = selectedErrCell.IsSchool == 1 ? "是" : "否"; // 是否学校
            cmbIsSchool.Enabled = false; // 是否学校不可编辑

        }

        internal void SetErrCell(KpiTables.ErrCell selectedErrCell, string SetMode, ErrCellAnalysis errCellAnalysis = null!)
        {
            // 初始化
            SetErrCell(selectedErrCell, errCellAnalysis);
            // 初始化失败
            if (string.IsNullOrEmpty(txbKeyId.Text.Trim())) { return; }

            switch (SetMode)
            {
                case "潮汐":
                    txbProblem.Text = "潮汐场景";
                    cmbProblemLevel1.Text = "用户";
                    cmbProblemLevel2.Text = "潮汐";
                    txbMeasure.Text = "制定潮汐小区处理方案(小区合并/临时停闭)";
                    cmbMeasureType.Text = "观察";
                    cmbResponsibleDept.Text = "优化室";
                    cmbResponsibleSpecialty.Text = "优化";
                    cmbResponsiblePerson.Text = "丁显强";
                    dtpProcessTime.Value = dtpProcessTime.MinDate;
                    dtpConfirmTime.Value = dtpConfirmTime.MinDate;
                    cmbStatus.Text = "分发处理";
                    break;
                case "新兴区域-规划":
                    cmbProblemLevel1.Text = "规划建设";
                    cmbProblemLevel2.Text = "规划";
                    txbMeasure.Text = "观察,等待用户入驻";
                    cmbMeasureType.Text = "观察";
                    cmbResponsibleDept.Text = "优化室";
                    cmbResponsibleSpecialty.Text = "规划";
                    cmbResponsiblePerson.Text = "孙财";
                    dtpProcessTime.Value = dtpProcessTime.MinDate;
                    dtpConfirmTime.Value = dtpConfirmTime.MinDate;
                    cmbStatus.Text = "分发处理";
                    break;
                case "统计问题":
                    cmbProblemLevel1.Text = "优化原因";
                    cmbProblemLevel2.Text = "统计问题";
                    txbMeasure.Text = "后台修复";
                    cmbMeasureType.Text = "后台修复";
                    cmbResponsibleDept.Text = "优化室";
                    cmbResponsibleSpecialty.Text = "优化";
                    cmbResponsiblePerson.Text = "丁显强";
                    cmbStatus.Text = "分发处理";
                    break;
                case "干扰问题":
                    cmbProblemLevel1.Text = "优化原因";
                    cmbProblemLevel2.Text = "干扰闭锁";
                    txbMeasure.Text = "后台排查";
                    cmbMeasureType.Text = "后台排查";
                    cmbResponsibleDept.Text = "优化室";
                    cmbResponsibleSpecialty.Text = "优化";
                    cmbResponsiblePerson.Text = "涂清豪";
                    cmbStatus.Text = "分发处理";
                    break;
                case "质差问题":
                    cmbProblemLevel1.Text = "优化原因";
                    cmbProblemLevel2.Text = "质差闭锁";
                    txbMeasure.Text = "后台排查";
                    cmbMeasureType.Text = "后台排查";
                    cmbResponsibleDept.Text = "优化室";
                    cmbResponsibleSpecialty.Text = "优化";
                    cmbResponsiblePerson.Text = "涂清豪";
                    cmbStatus.Text = "分发处理";
                    break;
                case "参数问题":
                    cmbProblemLevel1.Text = "优化原因";
                    cmbProblemLevel2.Text = "参数问题";
                    cmbMeasureType.Text = "参数调整";
                    cmbResponsibleDept.Text = "优化室";
                    cmbResponsibleSpecialty.Text = "优化";
                    cmbResponsiblePerson.Text = "丁显强";
                    cmbStatus.Text = "分发处理";
                    break;
                case "小区合并":
                    cmbProblemLevel1.Text = "用户";
                    cmbProblemLevel2.Text = "潮汐";
                    cmbMeasureType.Text = "小区合并";
                    cmbResponsibleDept.Text = "优化室";
                    cmbResponsibleSpecialty.Text = "优化";
                    cmbResponsiblePerson.Text = "丁显强";
                    cmbStatus.Text = "分发处理";
                    break;
                case "现场测试":
                    cmbProblemLevel1.Text = "用户";
                    cmbProblemLevel2.Text = "用户少";
                    cmbMeasureType.Text = "现场测试";
                    cmbResponsibleDept.Text = "优化室";
                    cmbResponsibleSpecialty.Text = "优化";
                    cmbResponsiblePerson.Text = "丁显强";
                    cmbStatus.Text = "分发处理";
                    break;
                case "维护-无线":
                    cmbProblemLevel1.Text = "故障";
                    cmbProblemLevel2.Text = "设备";
                    txbMeasure.Text = "故障处理";
                    cmbMeasureType.Text = "故障处理";
                    cmbResponsibleDept.Text = "维护室";
                    cmbResponsibleSpecialty.Text = "无线";
                    cmbResponsiblePerson.Text = "胡赟";
                    cmbStatus.Text = "分发处理";
                    break;
                case "维护-传输":
                    cmbProblemLevel1.Text = "故障";
                    cmbProblemLevel2.Text = "传输";
                    txbMeasure.Text = "故障处理";
                    cmbMeasureType.Text = "故障处理";
                    cmbResponsibleDept.Text = "维护室";
                    cmbResponsibleSpecialty.Text = "传输";
                    cmbResponsiblePerson.Text = "彭家华";
                    cmbStatus.Text = "分发处理";
                    break;
                case "维护-停业":
                    cmbProblemLevel1.Text = "纠纷";
                    cmbProblemLevel2.Text = "停业";
                    txbMeasure.Text = "拆除闲置设备";
                    cmbMeasureType.Text = "设备拆除";
                    cmbResponsibleDept.Text = "维护室";
                    cmbResponsibleSpecialty.Text = "无线";
                    cmbResponsiblePerson.Text = "胡赟";
                    cmbStatus.Text = "分发处理";
                    break;
                case "维护-网调-室分":
                    cmbProblemLevel1.Text = "故障";
                    cmbProblemLevel2.Text = "设备";
                    txbMeasure.Text = "故障处理";
                    cmbMeasureType.Text = "故障处理";
                    cmbResponsibleDept.Text = "维护室";
                    cmbResponsibleSpecialty.Text = "网调(室分)";
                    cmbResponsiblePerson.Text = "胡赟";
                    cmbStatus.Text = "分发处理";
                    break;
                case "维护-网调-宏站":
                    cmbProblemLevel1.Text = "故障";
                    cmbProblemLevel2.Text = "设备";
                    txbMeasure.Text = "故障处理";
                    cmbMeasureType.Text = "故障处理";
                    cmbResponsibleDept.Text = "维护室";
                    cmbResponsibleSpecialty.Text = "网调(宏站)";
                    cmbResponsiblePerson.Text = "胡赟";
                    cmbStatus.Text = "分发处理";
                    break;
                case "维护-铁塔":
                    cmbProblemLevel1.Text = "故障";
                    cmbProblemLevel2.Text = "设备";
                    txbMeasure.Text = "故障处理";
                    cmbMeasureType.Text = "故障处理";
                    cmbResponsibleDept.Text = "维护室";
                    cmbResponsibleSpecialty.Text = "铁塔";
                    cmbResponsiblePerson.Text = "胡赟";
                    cmbStatus.Text = "分发处理";
                    break;
                case "维护-动力":
                    cmbProblemLevel1.Text = "故障";
                    cmbProblemLevel2.Text = "设备";
                    txbMeasure.Text = "故障处理";
                    cmbMeasureType.Text = "故障处理";
                    cmbResponsibleDept.Text = "维护室";
                    cmbResponsibleSpecialty.Text = "动力";
                    cmbResponsiblePerson.Text = "刘浩";
                    cmbStatus.Text = "分发处理";
                    break;
                case "资管问题":
                    cmbProblemLevel1.Text = "其他";
                    cmbProblemLevel2.Text = "资管问题";
                    txbMeasure.Text = "修改资管";
                    cmbMeasureType.Text = "修改资管";
                    cmbResponsibleDept.Text = "维护室";
                    cmbResponsibleSpecialty.Text = "资管";
                    cmbResponsiblePerson.Text = "胡赟";
                    cmbStatus.Text = "分发处理";
                    break;
                case "工程-室分":
                    cmbProblemLevel1.Text = "故障";
                    cmbProblemLevel2.Text = "设备";
                    txbMeasure.Text = "故障处理";
                    cmbMeasureType.Text = "故障处理";
                    cmbResponsibleDept.Text = "工程室";
                    cmbResponsibleSpecialty.Text = "工程(室分)";
                    cmbResponsiblePerson.Text = "殷玉林";
                    cmbStatus.Text = "分发处理";
                    break;
                case "工程-宏站":
                    cmbProblemLevel1.Text = "故障";
                    cmbProblemLevel2.Text = "设备";
                    txbMeasure.Text = "故障处理";
                    cmbMeasureType.Text = "故障处理";
                    cmbResponsibleDept.Text = "工程室";
                    cmbResponsibleSpecialty.Text = "工程(宏站)";
                    cmbResponsiblePerson.Text = "殷玉林";
                    cmbStatus.Text = "分发处理";
                    break;
                case "工程-传输":
                    cmbProblemLevel1.Text = "故障";
                    cmbProblemLevel2.Text = "传输";
                    txbMeasure.Text = "故障处理";
                    cmbMeasureType.Text = "故障处理";
                    cmbResponsibleDept.Text = "工程室";
                    cmbResponsibleSpecialty.Text = "传输";
                    cmbResponsiblePerson.Text = "粱其君";
                    cmbStatus.Text = "分发处理";
                    break;
                case "工程-铁塔":
                    cmbProblemLevel1.Text = "故障";
                    cmbProblemLevel2.Text = "传输";
                    txbMeasure.Text = "故障处理";
                    cmbMeasureType.Text = "故障处理";
                    cmbResponsibleDept.Text = "工程室";
                    cmbResponsibleSpecialty.Text = "传输";
                    cmbResponsiblePerson.Text = "殷玉林";
                    cmbStatus.Text = "分发处理";
                    break;
                default:
                    break;

            }
        }

        // 通过 ErrNode 设置表单控件值
        internal void SetErrNode(KpiTables.ErrNode selectedErrNode, ErrCellAnalysis errCellAnalysis = null!)
        {
            if (_db == null)
            {
                MessageBox.Show("数据库连接失败！");
                return;
            }

            if (errCellAnalysis == null)
            {
                _errCellAnalysis = new ErrCellAnalysis();
            }
            else
            {
                _errCellAnalysis = errCellAnalysis;
            }

            _errNode = selectedErrNode;
            // 设置表单控件的值
            txbKeyId.Text = _errNode.KeyId; // 键值ID
            txbENI.Text = _errNode.ENI; // ENI
            txbGcNodeName.Text = _errNode.GcNodeName; // 工参基站
            txbCGI.Text = _errNode.CGI; // CGI
            dtpStartDate.Value = DateTime.TryParse(_errNode.StartDate, out DateTime startDate) ? startDate : DateTime.Now; // 开始日期
            txbProblem.Text = _errNode.Problem; // 问题描述
            cmbProblemLevel1.Text = _errNode.ProblemLevel1; // 问题一级
            cmbProblemLevel2.Text = _errNode.ProblemLevel2; // 问题二级
            cmbMeasureType.Text = _errNode.MeasureType; // 措施类型
            txbMeasure.Text = _errNode.Measure; // 措施内容 
            cmbResponsibleDept.Text = _errNode.ResponsibleDept; // 责任室
            cmbResponsibleSpecialty.Text = _errNode.ResponsibleSpecialty; // 责任专业
            cmbResponsiblePerson.Text = _errNode.ResponsiblePerson; // 责任人  
            dtpProcessTime.Value = string.IsNullOrEmpty(_errNode.ProcessTime)
                ? dtpProcessTime.MinDate
                : DateTime.TryParse(_errNode.ProcessTime, out DateTime processTime) ? processTime : dtpProcessTime.MinDate; // 处理时间
            cmbStatus.Text = _errNode.Status; // 状态
            dtpConfirmTime.Value = string.IsNullOrEmpty(_errNode.ConfirmTime)
                ? dtpConfirmTime.MinDate
                : DateTime.TryParse(_errNode.ConfirmTime, out DateTime confirmTime) ? confirmTime : dtpConfirmTime.MinDate; // 确认时间

            cmbIsSchool.Text = _errNode.IsSchool == 1 ? "是" : "否"; // 是否学校  

        }

        // 给 ComboBox 添加 MouseDown 事件处理程序
        // 使其在鼠标点击时展开下拉列表
        // 需要在窗体加载时绑定这些事件
        private void ComboBox_MouseDown(object? sender, MouseEventArgs e)
        {
            if (sender == null) { return; }
            if (sender is ComboBox combo)
            {
                combo.DroppedDown = true;
            }
        }

        private void txbCGI_TextChanged(object sender, EventArgs e)
        {
            string CGI = txbCGI.Text.Trim();
            string ENI = txbENI.Text.Trim();
            string GcNodeNmae = string.Empty;
            if (_db != null)
            {
                if (!string.IsNullOrEmpty(CGI))
                {
                    GcNodeNmae = _db.ExecuteScalar<string>($"select 工参小区 from gccgiinfo where cgi='{CGI}'");
                }
                if(string.IsNullOrEmpty(GcNodeNmae))
                {
                    GcNodeNmae = _db.ExecuteScalar<string>($"select 工参基站 from gccgiinfo where eni='{ENI}'");
                }
            }
            if (string.IsNullOrEmpty(CGI))
            {
                txbKeyId.Text = $"{DateTime.Now.ToString("yyyy-MM-dd")}_{CGI}";
                
            }
            else
            {
                txbKeyId.Text = $"{DateTime.Now.ToString("yyyy-MM-dd")}_{ENI}";
            }

            txbGcNodeName.Text = GcNodeNmae;
        }
    }
}


